
#ifndef __NEW_KEY_DB_H
#define __NEW_KEY_DB_H

#include <vector>
#include "SQLiteDB.h"
#include "KeyTableView.h"

#include "../crypt/EncryptionKey.h"
#include "../crypt/DecryptionKey.h"
#include "../crypt/SigningKey.h"
#include "../crypt/CheckingKey.h"

#define KEY_GENERATE_TYPE_ROOT_ENCRYPT_DECRYPT    Key::KEY_ASYMMETRIC_PAIR_RSA
#define KEY_GENERATE_TYPE_LEAF_ENCRYPT_DECRYPT    Key::KEY_SYMMETRIC_AES
#define KEY_GENERATE_TYPE_ROOT_CHECKING_SIGNING   Key::KEY_ASYMMETRIC_PAIR_RSA
#define KEY_GENERATE_TYPE_LEAF_CHECKING_SIGNING   Key::KEY_ASYMMETRIC_PAIR_RSA

#define ENCRYPTION_KEYTABLE_NAME "encryption_keys"
#define DECRYPTION_KEYTABLE_NAME "decryption_keys"
#define SIGNING_KEYTABLE_NAME    "signing_keys"
#define CHECKING_KEYTABLE_NAME   "checking_keys"

/** Class for manipulating a database of keys. */
class NewKeyDB : public SQLiteDB
{
   public:
      /** Types of marking for key roles and lifetime information. */
      enum keymarkstype {   KEYMARK_EXPENDABLE = 'E',
                            KEYMARK_CHAINLATEST = 'L',
                            KEYMARK_CHAIN = 'C',
                            KEYMARK_CHAINROOT = 'R',
                            KEYMARK_PERMANENT = 'P'    };
      enum expirymarktype {   EXPIRYMARK_DELETE = '0',
                              EXPIRYMARK_DELETEIN1 = '1',
                              EXPIRYMARK_DELETEIN2 = '2',
                              EXPIRYMARK_DELETEIN3 = '3'   };
      NewKeyDB(const char *filename, const char *WIPpwd, const char *ownname, const char *ownpwd, int persists);
      ~NewKeyDB();
      int generateEncDecPairIntoDB(int ischained, int isexpendable,
         const char *name, const char *pwd, const char *owner);
      int generateSgnChkPairIntoDB(int ischained, int isexpendable,
         const char *name, const char *pwd, const char *owner);
      static int generateNewEncryptionDecryptionKeys(int ischainroot, const char *name, EncryptionKey **enckey, DecryptionKey **deckey);
      static int generateNewSigningCheckingKeys(int ischainroot, const char *name, SigningKey **sgnkey, CheckingKey **chkkey);
   private:
      std::vector<const char *> *getTableNamesFromCreateTablesIfNecessary();
      /** Pointer to the key table view of the EncryptionKey storage. */
      KeyTableView *encryptionkeys;
      /** Pointer to the key table view of the DecryptionKey storage. */
      KeyTableView *decryptionkeys;
      /** Pointer to the key table view of the SigningKey storage. */
      KeyTableView *signingkeys;
      /** Pointer to the key table view of the CheckingKey storage. */
      KeyTableView *checkingkeys;
      /** Set of table names for the key. */
      std::vector<const char *> *keytablenames;
};

#endif
